//
//  RealmGlobalButton.swift
//  DaGlass
//
//  Created by 李然 on 2023/6/9.
//

import UIKit

class RealmGlobalButton: UIView {
    static let ButtonSize: CGFloat = 60
    static let SubButtonsViewSize: CGFloat = 300
    var superFrame = CGRect()// 上级view的frame
    
    init(frame: CGRect, viewFrame: CGRect) {
        var f = frame
        f.size.width = RealmGlobalButton.ButtonSize
        f.size.height = RealmGlobalButton.ButtonSize
        f.origin.x = frame.origin.x > viewFrame.size.width-frame.origin.x ? viewFrame.size.width-10-RealmGlobalButton.ButtonSize : 10
        super.init(frame: f)
        
        superFrame = viewFrame

        setupButton()
        self.backgroundColor = UIColor.clear
        
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureAction(pan:)))
        self.isUserInteractionEnabled = true
        
        self.addGestureRecognizer(panGesture)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupLaye(path: UIBezierPath, white: Float, alpha: Float) {
        let layer = CAShapeLayer()
        layer.path = path.cgPath
        layer.fillColor = UIColor(white: CGFloat(white), alpha: CGFloat(alpha)).cgColor
        layer.strokeColor = UIColor(white: CGFloat(white), alpha: CGFloat(alpha)).cgColor
        self.layer.addSublayer(layer)
    }
    
    func setupButton() {
        let centerP = CGPoint(x: frame.size.width/2, y: frame.size.height/2)
        var path = UIBezierPath()
        path.addArc(withCenter: centerP, radius: RealmGlobalButton.ButtonSize/2, startAngle: 0, endAngle: CGFloat.pi*2, clockwise: true)
        setupLaye(path: path, white: 0, alpha: 0.85)
        
        path = UIBezierPath()
        path.addArc(withCenter: centerP, radius: RealmGlobalButton.ButtonSize/2.7, startAngle: 0, endAngle: CGFloat.pi*2, clockwise: true)
        setupLaye(path: path, white: 1, alpha: 0.2)
        
        path = UIBezierPath()
        path.addArc(withCenter: centerP, radius: RealmGlobalButton.ButtonSize/3.4, startAngle: 0, endAngle: CGFloat.pi*2, clockwise: true)
        setupLaye(path: path, white: 1, alpha: 0.3)
        
        path = UIBezierPath()
        path.addArc(withCenter: centerP, radius: RealmGlobalButton.ButtonSize/4.8, startAngle: 0, endAngle: CGFloat.pi*2, clockwise: true)
        setupLaye(path: path, white: 1, alpha: 1)
    }
    
    // 拖拽
    @objc func panGestureAction(pan: UIPanGestureRecognizer) {
        switch pan.state {
        case UIGestureRecognizer.State.began:
            self.superview?.bringSubviewToFront(self)
        case UIGestureRecognizer.State.changed:
            let point = pan.translation(in: self)
            let f = self.frame
            let dx = point.x + f.origin.x
            let dy = point.y + f.origin.y
            self.frame = CGRect(x: dx, y: dy, width: RealmGlobalButton.ButtonSize, height: RealmGlobalButton.ButtonSize)
            //  注意一旦你完成上述的移动，将translation重置为0十分重要。否则translation每次都会叠加
            pan.setTranslation(CGPoint(x: 0, y: 0), in: self)
        case UIGestureRecognizer.State.ended:
            let f = self.frame
            var dx = f.origin.x
            var dy = f.origin.y
            if dx > superFrame.size.width-10-RealmGlobalButton.ButtonSize {
                dx = superFrame.size.width-10-RealmGlobalButton.ButtonSize
            } else if dx < 10 {
                dx = 10.0
            } else {
                dx = dx > superFrame.size.width-dx ? superFrame.size.width-10-RealmGlobalButton.ButtonSize : 10
            }

            if dy > superFrame.size.height-10-RealmGlobalButton.ButtonSize {
                dy = superFrame.size.height-10-RealmGlobalButton.ButtonSize
            } else if dy < 10 {
                dy = 10.0
            }
            
            UIView.animate(withDuration: 0.2) {
                self.frame = CGRect(x: dx, y: dy, width: f.size.width, height: f.size.height)
            }
        default:
            break
        }
        let point = pan.translation(in: self)
        self.transform = CGAffineTransform.init(translationX: point.x, y: point.y)
    }
}
